import WinXin from '@/dll/weixin'
// import baseUrl from '@/baseUrl'
import Enum from '@/config/enum'
import ShareGuide from '../components/shareGuide'
import Mall from '@/dll/mall'

export const share = {
  components: {
    ShareGuide
  },

  data () {
    return {
      link: '',
      defaultLink: '',
      isShowShare: false,
      Enum
    }
  },

  computed: {
    // 好评率
    rateAcclaim () {
      return this.product.good_comment_rate ? this.product.good_comment_rate : '暂无'
    },

    // 好评率详情
    ifComment () {
      return !!this.product.good_comment_rate
    }
  },

  created () {
    // this.defaultLink = window.location.href
    // this.clipBoardContent = this.link || this.defaultLink
    // this.clipBoardContent = location.href
    // if (this.isSanXia) {
    this.getShareUrl({ url_type: Enum.URL_TYPE.COLLEAGUE }, (data) => {
      let share = data.split('?')
      let shareParm = window.location.hash + '&' + share[1]
      let shareHost = share[0]
      this.defaultLink = `${shareHost}/${shareParm}`
      this.clipBoardContent = this.defaultLink
    })
    // }
    // else {
    //   this.getShareUrl({ url_type: Enum.URL_TYPE.FRIEND }, (data) => {
    //     this.defaultLink = `${data}/goods_sku/${this.$route.params.id}?VNK=${this.$route.query.VNK}`
    //     this.clipBoardContent = this.defaultLink
    //   })
    // }
  },

  methods: {
    // 菜单回调
    menusFun (key, item) {
      let fun = {
        shareAppMessage: () => {
          let config = {
            type: 1,
            shareTitle: this.product.goods_name,
            shareDescr: this.product.subtitle,
            shareThumbImage: this.product.goods_image_main,
            // shareWebPageUrl: location.href
            // shareWebPageUrl: baseUrl.link.SHARE + this.$route.params.id
            shareWebPageUrl: this.link || this.defaultLink
          }
          WinXin.ShareToWeiXin(config, () => {
          })
        },
        shareTimeline: () => {
          let config = {
            type: 2,
            shareTitle: this.product.goods_name,
            shareDescr: this.product.subtitle,
            shareThumbImage: this.product.goods_image_main,
            // shareWebPageUrl: location.href
            // shareWebPageUrl: baseUrl.link.SHARE + this.$route.params.id
            shareWebPageUrl: this.link || this.defaultLink
          }
          WinXin.ShareToWeiXin(config, () => {
          })
        },
        copy: () => {
          let input = document.getElementById('clipBoardContent')
          const range = document.createRange()
          range.selectNode(input)
          const selection = window.getSelection()
          if (selection.rangeCount > 0) {
            selection.removeAllRanges()
          }
          selection.addRange(range)
          // input.select();
          // input.setSelectionRange(0, input.value.length);
          let isCopy = document.execCommand('copy')
          if (isCopy) {
            this.$vux.alert.show({
              title: '提示',
              content: '连接已复制,去粘贴吧！'
            })
          } else {
            this.$vux.alert.show({
              title: '提示',
              content: '浏览器不支持，请手动复制。         '
            })
          }
        }
      }
      if (fun[item.value]) {
        fun[item.value]()
      }
    },

    // 更新微信分享
    updateAppMessageShareData () {
      let config = {
        title: this.product.goods_name,
        desc: this.product.subtitle,
        // link: location.href,
        link: this.link || this.defaultLink,
        imgUrl: this.product.goods_image_main,
        success: () => {
        }
      }
      WinXin.updateAppMessageShareData(config)
    },

    // 分享链接
    async share (type) {
      let param = {
        url_type: type
      }
      let { FRIEND, COLLEAGUE } = this.Enum.URL_TYPE
      this.getShareUrl(param, (data) => {
        if (data) {
          this.isShowShare = true
          if (FRIEND === type) { // 分享给朋友
            this.link = `${data}/goods_sku/${this.$route.params.id}?VNK=${this.$route.query.VNK}`
          } else if (COLLEAGUE === type) { // 分享给同事
            let share = data.split('?')
            let shareParm = window.location.hash + '&' + share[1]
            let shareHost = share[0]
            this.link = `${shareHost}/${shareParm}`
          }
          this.clipBoardContent = this.link
          this.updateAppMessageShareData()
        }
      })
    },

    // 获取分享url
    async getShareUrl (param, callback) {
      this.$vux.loading.show({
        text: '请稍后'
      })
      let res = await Mall.getShareUrl(param)
      this.$vux.loading.hide()
      if (res.code == 0) {
        callback(res.data)
      } else {
        this.$vux.alert.show({
          title: '提示',
          content: res.msg
        })
      }
    },

    // 设置图片大小
    setImg () {
      setTimeout(() => {
        this.$nextTick(function () {
          let iosweixin = document.querySelector('#iosweixin')
          if (!iosweixin) return
          let ssdModule = iosweixin.querySelectorAll('.ssd-module')
          if (!ssdModule.length) return
          let arrSsaModule = Array.prototype.slice.call(ssdModule)
          arrSsaModule.forEach(function (item, index) {
            let styleHeight = getComputedStyle(item).height
            item.style.height = (parseInt(styleHeight) / 2) + 'px'
          })
        })
      }, 500)
    },

    // 进入评论列表
    onComment () {
      // if (!this.ifComment) return
      this.$router.push({ name: 'mallComment', params: { goods_sku: this.product.goods_sku } })
    }
  },

  mounted () {
  }

}
